Skip to main content

Get CPU Frequency

Description

The get_cpu_frequency function retrieves the current CPU frequency in MHz by reading the /proc/cpuinfo file and searching for the "cpu MHz" entry.

Function Signature:

def get_cpu_frequency() -> float:

Parameters

  • None

Returns

  • float: The CPU frequency in MHz.

Example Usage

cpu_frequency = get_cpu_frequency()
print(f"Current CPU frequency: {cpu_frequency} MHz")

Notes

  • This function reads the contents of /proc/cpuinfo to extract the CPU frequency.
  • It uses a regular expression (re.search) to locate the "cpu MHz" entry and retrieve the frequency.

Error Handling

  • If the "cpu MHz" entry is not found in the /proc/cpuinfo file, a RuntimeError will be raised indicating that the CPU frequency could not be retrieved.